<?php

namespace app\admin\controller\Business;

use app\common\controller\Backend;

/**
 * 客户管理
 *
 * @icon fa fa-circle-o
 */
class Info extends Backend
{
    protected $model = null;

    //开启关联搜索
    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Business.Business');
    }

    //客户资料 
    public function index($ids = NULL)
    {   
        //连表查询
        $row = $this->model->with(['source', 'admin', 'parent'])->find($ids);

        if(!$row)
        {
            $this->error(__('No results were found'));
            exit;
        }

        $this->assign('row', $row);
        
        return $this->view->fetch();
    }

     //申请记录
     public function receive($ids = null)
     {
         $this->model = model('Business.Receive');
 
         //设置过滤方法
         $this->request->filter(['strip_tags', 'trim']);
 
         if ($this->request->isAjax())
         {   
             if ($this->request->request('keyField'))
             {
                 return $this->selectpage();
             }
             
             list($where, $sort, $order, $offset, $limit) = $this->buildparams();
 
             $AuthWhere = ['busid' => $ids];
 
             $total = $this->model
                 ->with(['admin', 'business'])
                 ->where($where)
                 ->where($AuthWhere)
                 ->order($sort, $order)
                 ->limit($offset, $limit)
                 ->count();
 
             $list = $this->model
                 ->with(['admin', 'business'])
                 ->where($where)
                 ->where($AuthWhere)
                 ->order($sort, $order)
                 ->limit($offset, $limit)
                 ->select();
 
             $result = array("total" => $total, "rows" => $list);
 
             return json($result);
         }
     }

    //回访记录
    public function visit($ids = null)
    {
        $this->model = model('Business.Visit');

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) 
        {   
            if ($this->request->request('keyField')) 
            {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $AuthWhere = ['busid' => $ids];
        
            $total = $this->model
                ->with(['admin', 'business'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->with(['admin', 'business'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
    }

    //消费记录
    public function record($ids = null)
    {
        $this->model = model('Business.Record');

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax())
        {   
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $AuthWhere = ['busid' => $ids];

            $total = $this->model
                ->with(['business'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->with(['business'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
    }

     //评论记录
     public function comment($ids = null)
     {
         $this->model =new \app\common\model\Subject\Comment;
 
         //设置过滤方法
         $this->request->filter(['strip_tags', 'trim']);
 
         if ($this->request->isAjax())
         {   
             if ($this->request->request('keyField'))
             {
                 return $this->selectpage();
             }
             
             list($where, $sort, $order, $offset, $limit) = $this->buildparams();
 
             $AuthWhere = ['busid' => $ids];
 
             $total = $this->model
                 ->with(['subject', 'business'])
                 ->where($where)
                 ->where($AuthWhere)
                 ->order($sort, $order)
                 ->limit($offset, $limit)
                 ->count();
 
             $list = $this->model
                 ->with(['subject', 'business'])
                 ->where($where)
                 ->where($AuthWhere)
                 ->order($sort, $order)
                 ->limit($offset, $limit)
                 ->select();
 
             $result = array("total" => $total, "rows" => $list);
 
             return json($result);
         }
     }

     //佣金领取记录
     public function commission($ids = null)
       {
           $this->model = model('Business.Commission');
   
           //设置过滤方法
           $this->request->filter(['strip_tags', 'trim']);
   
           if ($this->request->isAjax())
           {   
               if ($this->request->request('keyField'))
               {
                   return $this->selectpage();
               }
               
               list($where, $sort, $order, $offset, $limit) = $this->buildparams();
   
               $AuthWhere = ['commission.busid' => $ids];
   
               $total = $this->model
                   ->with(['parent', 'business','order'])
                   ->where($where)
                   ->where($AuthWhere)
                   ->order($sort, $order)
                   ->limit($offset, $limit)
                   ->count();
   
               $list = $this->model
                   ->with(['parent', 'business','order'])
                   ->where($where)
                   ->where($AuthWhere)
                   ->order($sort, $order)
                   ->limit($offset, $limit)
                   ->select();
   
               $result = array("total" => $total, "rows" => $list);
   
               return json($result);
           }
       }

    //优惠券领取订单
    public function coupon($ids = null)
       {
           $this->model = model('Coupon.Receive');
   
           //设置过滤方法
           $this->request->filter(['strip_tags', 'trim']);
   
           if ($this->request->isAjax())
           {   
               if ($this->request->request('keyField'))
               {
                   return $this->selectpage();
               }
               
               list($where, $sort, $order, $offset, $limit) = $this->buildparams();
   
               $AuthWhere = ['busid' => $ids];
   
               $total = $this->model
                   ->with(['business','coupon'])
                   ->where($where)
                   ->where($AuthWhere)
                   ->order($sort, $order)
                   ->limit($offset, $limit)
                   ->count();
   
               $list = $this->model
                   ->with(['business','coupon'])
                   ->where($where)
                   ->where($AuthWhere)
                   ->order($sort, $order)
                   ->limit($offset, $limit)
                   ->select();
   
               $result = array("total" => $total, "rows" => $list);
   
               return json($result);
           }
       }
    
    //课程订单
    public function order($ids = null)
       {
           $this->model =new \app\common\model\Subject\Order;
   
           //设置过滤方法
           $this->request->filter(['strip_tags', 'trim']);
   
           if ($this->request->isAjax())
           {   
               if ($this->request->request('keyField'))
               {
                   return $this->selectpage();
               }
               
               list($where, $sort, $order, $offset, $limit) = $this->buildparams();
   
               $AuthWhere = ['busid' => $ids];
   
               $total = $this->model
                   ->with(['business','subject','category'])
                   ->where($where)
                   ->where($AuthWhere)
                   ->order($sort, $order)
                   ->limit($offset, $limit)
                   ->count();
   
               $list = $this->model
                   ->with(['business','subject','category'])
                   ->where($where)
                   ->where($AuthWhere)
                   ->order($sort, $order)
                   ->limit($offset, $limit)
                   ->select();
   
               $result = array("total" => $total, "rows" => $list);
   
               return json($result);
           }
       }
}
